﻿using HotelManager.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HotelManager.Service
{
    public class AdminService:IAdminService
    {
        private readonly HttpRestClient client;
        public AdminService(HttpRestClient client)
        {
            this.client = client;
        }
        public UserInfosData GetAll(int roleId, int pageIndex, int pageSize)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Get;
            StringBuilder sb= new StringBuilder();
            if (pageIndex==0||pageSize==0)
            {
                sb.Append("&pageIndex=").Append(1).Append("&pageSize=").Append(10);
            }
            else
            {
                sb.Append("&pageIndex=").Append(pageIndex).Append("&pageSize=").Append(pageSize);
            }
            
            request.Route = $"api/Admin/List?roleId={roleId}"+sb.ToString();
            return client.ExecuteGetSync<UserInfosData>(request);
        }
        public async Task<UserData> GetFirstOrDefaultAsync(string loginId)
        {
            BaseRequest request = new BaseRequest();
            UserIdData userIdBody = new UserIdData() { loginId = loginId };
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Admin/GetOne";
            request.Parameter = userIdBody;
            return await client.ExecutePostAsync<UserData>(request);
        }
        public async Task<ResultData<string>> DeleteAsync(AdminDeleteData adminDeleteBody)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Admin/Delete";
            request.Parameter = adminDeleteBody;
            return await client.ExecutePostAsync<ResultData<string>>(request);
        }
        public ResultData<string> UploadImage(string filePath)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Admin/UploadImg";
            //request.Parameter = adminDeleteBody;
            return client.ExecuteUpload<ResultData<string>>(request, filePath);
        }
        public async Task<ResultData<string>> AddAsync(UserData userData)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Admin/Add";
            request.Parameter = userData;
            return await client.ExecutePostAsync<ResultData<string>>(request);
        }
        public ResultData<string> Update(UserData userData)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Admin/Update";
            request.Parameter = userData;
            return client.ExecuteSync<ResultData<string>>(request);
        }
    }
}
